package com.www.applyword.ui.chat;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;

import com.tencent.imsdk.TIMConversationType;
import com.tencent.imsdk.TIMFriendshipManager;
import com.tencent.imsdk.TIMValueCallBack;
import com.tencent.imsdk.friendship.TIMFriendResult;
import com.tencent.qcloud.tim.uikit.component.CircleImageView;
import com.tencent.qcloud.tim.uikit.component.LineControllerView;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.tencent.qcloud.tim.uikit.modules.contact.ContactItemBean;
import com.tencent.qcloud.tim.uikit.modules.contact.FriendProfileLayout;
import com.tencent.qcloud.tim.uikit.utils.TUIKitConstants;
import com.tencent.qcloud.tim.uikit.utils.ToastUtil;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.bean.BookFriendBean;
import com.www.applyword.bean.LoginBean;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.main.MainActivity;
import com.www.applyword.utils.ImageUtil;
import com.www.applyword.utils.SPUtil;
import com.www.applyword.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author : huang
 * date   : 2019/11/16 0016 11:16
 * version: 1.0
 */
public class FriendProfileActivity extends BaseActivity {


    private String mId;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.contact_friend_profile_activity);
        setTitle("详细资料");
    }

    @Override
    protected void loadData() {
        FriendProfileLayout layout = findViewById(R.id.friend_profile);
        LineControllerView mRemarkView = findViewById(R.id.remark);

        LineControllerView blackList = findViewById(R.id.blackList);
        blackList.setCheckListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    addBlack();
                } else {
                    deleteBlack();
                }

            }
        });
        Object data = getIntent().getSerializableExtra(TUIKitConstants.ProfileType.CONTENT);
        layout.initData(data);
        if (data instanceof ContactItemBean) {
            ContactItemBean  mContactInfo = (ContactItemBean) data;
            mId = mContactInfo.getId();
        }

        CircleImageView mHeadImageView = layout.findViewById(R.id.avatar);
        String url = getIntent().getStringExtra("faceUrl");
        if (!TextUtils.isEmpty(url)) {
            ImageUtil.setImage(mHeadImageView, url);
        }
        mRemarkView.setVisibility(View.GONE);
        layout.setOnButtonClickListener(new FriendProfileLayout.OnButtonClickListener() {
            @Override
            public void onStartConversationClick(ContactItemBean info) {
                ChatInfo chatInfo = new ChatInfo();
                chatInfo.setType(TIMConversationType.C2C);
                chatInfo.setId(info.getId());
                String chatName = info.getId();
                if (!TextUtils.isEmpty(info.getRemark())) {
                    chatName = info.getRemark();
                } else if (!TextUtils.isEmpty(info.getNickname())) {
                    chatName = info.getNickname();
                }
                chatInfo.setChatName(chatName);
                Intent intent = new Intent(FriendProfileActivity.this, ChatActivity.class);
                intent.putExtra(Constants.CHAT_INFO, chatInfo);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }

            @Override
            public void onDeleteFriendClick(String id) {
                Map<String, Object> params = new HashMap<>();
                params.put("KeywordList", id);
                params.put("Type", "1");
                params.put("UserID", SPUtil.getString(Constants.USERID));
                HttpClient.post(FriendProfileActivity.this, Constants.GETMESSAGEUSERLIST, params, new CallBack<List<BookFriendBean>>() {
                    @Override
                    public void onSuccess(List<BookFriendBean> bookFriendBeans) {
                        Map<String, Object> params = new HashMap<>();
                        params.put("UserID", SPUtil.getString(Constants.USERID));
                        for (BookFriendBean bookFriendBean : bookFriendBeans) {
                            if (bookFriendBean.getMobile().equals(id)) {
                                params.put("FriendID", bookFriendBean.getUserID());
                            }
                        }
                        params.put("Type", 2 + "");
                        HttpClient.get(FriendProfileActivity.this, Constants.OPERATIONFRIEND, params, new CallBack<LoginBean>() {
                            @Override
                            public void onSuccess(LoginBean result) {
                                Utils.showToastSafe("删除好友成功");
                                Intent intent = new Intent(FriendProfileActivity.this, MainActivity.class);
                                startActivity(intent);
                            }
                        });
                    }
                });
            }
        });
    }

    private void deleteBlack() {
        List<String> idList = new ArrayList<>();
        idList.add(mId);
        TIMFriendshipManager.getInstance().deleteBlackList(idList, new TIMValueCallBack<List<TIMFriendResult>>() {
            @Override
            public void onError(int i, String s) {
               // TUIKitLog.e(TAG, "deleteBlackList err code = " + i + ", desc = " + s);
                ToastUtil.toastShortMessage("Error code = " + i + ", desc = " + s);
            }

            @Override
            public void onSuccess(List<TIMFriendResult> timFriendResults) {
              //  TUIKitLog.i(TAG, "deleteBlackList success");
                Map<String, Object> params = new HashMap<>();
                params.put("KeywordList", mId);
                params.put("Type", "1");
                params.put("UserID", SPUtil.getString(Constants.USERID));
                HttpClient.post(FriendProfileActivity.this, Constants.GETMESSAGEUSERLIST, params, new CallBack<List<BookFriendBean>>() {
                    @Override
                    public void onSuccess(List<BookFriendBean> bookFriendBeans) {
                        Map<String, Object> params = new HashMap<>();
                        params.put("UserID", SPUtil.getString(Constants.USERID));
                        for (BookFriendBean bookFriendBean : bookFriendBeans) {
                            if (bookFriendBean.getMobile().equals(mId)) {
                                params.put("FriendID", bookFriendBean.getUserID());
                            }
                        }
                        params.put("Type", 2 + "");  //2解除好友关系
                        HttpClient.get(FriendProfileActivity.this, Constants.OPERATIONFRIEND, params, new CallBack<LoginBean>() {
                            @Override
                            public void onSuccess(LoginBean result) {

                            }
                        });
                    }
                });
            }
        });
    }

    private void addBlack() {
        List<String> idList = new ArrayList<>();
        idList.add(mId);

        TIMFriendshipManager.getInstance().addBlackList(idList, new TIMValueCallBack<List<TIMFriendResult>>() {
            @Override
            public void onError(int i, String s) {
              // TUIKitLog.e(TAG, "addBlackList err code = " + i + ", desc = " + s);
                ToastUtil.toastShortMessage("Error code = " + i + ", desc = " + s);
            }

            @Override
            public void onSuccess(List<TIMFriendResult> timFriendResults) {
              //  TUIKitLog.i(TAG, "addBlackList success");
                Map<String, Object> params = new HashMap<>();
                params.put("KeywordList", mId);
                params.put("Type", "1");
                params.put("UserID", SPUtil.getString(Constants.USERID));
                HttpClient.post(FriendProfileActivity.this, Constants.GETMESSAGEUSERLIST, params, new CallBack<List<BookFriendBean>>() {
                    @Override
                    public void onSuccess(List<BookFriendBean> bookFriendBeans) {
                        Map<String, Object> params = new HashMap<>();
                        params.put("UserID", SPUtil.getString(Constants.USERID));
                        for (BookFriendBean bookFriendBean : bookFriendBeans) {
                            if (bookFriendBean.getMobile().equals(mId)) {
                                params.put("FriendID", bookFriendBean.getUserID());
                            }
                        }
                        params.put("Type", 1 + "");
                        HttpClient.get(FriendProfileActivity.this, Constants.OPERATIONFRIEND, params, new CallBack<LoginBean>() {
                            @Override
                            public void onSuccess(LoginBean result) {
                               // Utils.showToastSafe("删除好友成功");
                               // Intent intent = new Intent(FriendProfileActivity.this, MainActivity.class);
                               // startActivity(intent);
                            }
                        });
                    }
                });
            }
        });
    }
}
